package com.course.domain.service.impl;

import com.course.domain.dao.ClassPlanDao;
import com.course.domain.model.ClassPlan;
import com.course.domain.service.ClassService;
import com.course.util.DateUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by Justin on 2017/5/25.
 */
@Service
public class ClassServiceImpl implements ClassService {

    private final ClassPlanDao planDao;

    public ClassServiceImpl(ClassPlanDao planDao) {
        this.planDao = planDao;
    }

    @Override
    public ClassPlan createPlan(ClassPlan plan) {
        return planDao.save(plan);
    }

    @Override
    public ClassPlan deletePlan(ClassPlan plan) {
        planDao.delete(plan);
        return plan;
    }

    @Override
    public ClassPlan updatePlan(ClassPlan plan) {
        return planDao.save(plan);
    }

    @Override
    @Transactional
    public int updateTeacher(int planId, int teacherId) {
        return planDao.updateTeacher(planId, teacherId);
    }

    @Override
    @Transactional
    public int updateRoom(int planId, String room) {
        return planDao.updateRoom(planId, room);
    }

    @Override
    @Transactional
    public int updateMark(int planId, String mark) {
        return planDao.updateMark(planId, mark);
    }

    @Override
    public ClassPlan loadPlan(int planId) {
        return planDao.findOne(planId);
    }

    @Override
    public List<ClassPlan> findPlanByStudentIdAndCourseId(int studentId, int courseId) {
        return planDao.findByStudentIdAndCourseIdOrderByDate(studentId, courseId);
    }

    @Override
    public List<ClassPlan> findPlanByTeacherId(int teacherId) {
        return planDao.findByTeacherIdOrderByDate(teacherId);
    }

    @Override
    public ClassPlan findNextPlanByStudentIdAndCourseId(int studentId, int courseId) {
        return planDao.findFirstByStudentIdAndCourseIdAndDateAfterOrderByDate(studentId, courseId, DateUtils.now());
    }

    @Override
    public ClassPlan findNextPlanByTeacherId(int teacherId) {
        return planDao.findFirstByTeacherIdAndDateAfterOrderByDate(teacherId, DateUtils.now());
    }

    @Override
    public List<ClassPlan> findMarkByStudentIdAndCourseId(int studentId, int courseId) {
        return planDao.findByStudentIdAndCourseIdAndStateOrderByDate(studentId, courseId, 1);
    }

    @Override
    public Page<ClassPlan> PageMarkByStudentIdAndCourseId(int studentId, int courseId, Pageable pageable) {
        return planDao.findByStudentIdAndCourseIdAndState(studentId, courseId, 1, pageable);
    }
}
